﻿package com.designoutput.manager
{
    import com.designoutput.enum.ToolType;
    import com.designoutput.icon.ArrowRotPress;
    import com.designoutput.icon.CurrCords;
    import com.designoutput.icon.Move;
    import com.designoutput.icon.Picker;
    import com.designoutput.icon.ResizeHeight;
    import com.designoutput.icon.ResizeHeightWidth;
    import com.designoutput.icon.ResizeWidth;
    import com.designoutput.icon.ResizeWidthHeight;
    import com.designoutput.icon.RotateCursor;
    import com.designoutput.icon.SelectionMove;
    
    import flash.display.Bitmap;
    import flash.display.Stage;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.ui.Mouse;
    
    public class CursorManager extends Bitmap
    {
        private var _xOffset:int = 0;
        private var _yOffset:int = 0;
		private var _loocked:Boolean = false;
		
		private var _lastStage:Stage;

        public function CursorManager()
        {
			this.addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler, false, 0, true);
			this.addEventListener(Event.REMOVED_FROM_STAGE, removedFromStageHandler, false, 0, true);
        }
		
		private function addedToStageHandler(e:Event):void {
			stage.addEventListener(MouseEvent.MOUSE_MOVE, stage_mouseMoveHandler, false, 0, true);
			_lastStage = stage;
		}
		
		private function removedFromStageHandler(e:Event):void {
			_lastStage.removeEventListener(MouseEvent.MOUSE_MOVE, stage_mouseMoveHandler, false);
			_lastStage = null;
		}
		
		private function stage_mouseMoveHandler(e:Event):void {
			updateCursorPosition();	
		}
		
		private function updateCursorPosition():void {
			x = stage.mouseX + _xOffset;
			y = stage.mouseY + _yOffset;
		}
		
		public function lock():void {
			_loocked = true;
		}
		
		public function unlock():void {
			_loocked = false;
		}

        public function setCursor(toolType:String) : void
        {
			if(this._loocked)
				return;
            
            Mouse.hide();
            this.bitmapData = null;
            switch(toolType)
            {
				case ToolType.SelectionMove:
					this.bitmapData = new SelectionMove(0,0);
					this._xOffset = -1;
					this._yOffset = 0;
					break;
				case ToolType.CurrCords:
					this.bitmapData = new CurrCords(0,0);
					this._xOffset = -1;
					this._yOffset = 0;
					break;
					break;
				case ToolType.ResizeHeightWidth:
				{
					this._xOffset = -7;
					this._yOffset = -7;
					this.bitmapData = new ResizeHeightWidth(0, 0);
					break;
				}
				case ToolType.HandleL:
				case ToolType.HandleR:
					this._xOffset = -7;
					this._yOffset = -3;
					this.bitmapData = new ResizeWidth(0, 0);
					break;
				case ToolType.HandleB:
				case ToolType.HandleT:
					this._xOffset = -3;
					this._yOffset = -7;
					this.bitmapData = new ResizeHeight(0, 0);
					break;
				case ToolType.HandleTR:
				case ToolType.HandleBL:
				case ToolType.LineHandleLow:
				case ToolType.LineHandleHi:
					this._xOffset = -7;
					this._yOffset = -7;
					this.bitmapData = new ResizeWidthHeight(0, 0);
					break;
				case ToolType.HandleTL:
				case ToolType.HandleBR:
					this._xOffset = -7;
					this._yOffset = -7;
					this.bitmapData = new ResizeHeightWidth(0, 0);
					break;
				case ToolType.HandleRotate:
					this._xOffset = -7;
					this._yOffset = -7;
					this.bitmapData = new RotateCursor(0, 0);
					break;
				case ToolType.ElementBody:
					this._xOffset = -1;
					this._yOffset = -0;
					this.bitmapData = new Move(0, 0);
					break;
				case ToolType.HandleRotateOnPress:
					this._xOffset = -1;
					this._yOffset = -0;
					this.bitmapData = new ArrowRotPress(0, 0);
					break;
				case ToolType.Picker:
					this._xOffset = -2;
					this._yOffset = -15;
					this.bitmapData = new Picker(0, 0);
					break;
                default:
					Mouse.show();
					break;
            }
			updateCursorPosition();
        }
    }
}
